package com.linkoog.devtools.http.request.http_request;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.project.Project;
import com.intellij.ui.Gray;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.tabs.TabInfo;
import com.intellij.ui.tabs.impl.JBEditorTabs;
import com.intellij.util.ui.JBUI;
import com.linkoog.devtools.http.request.common.RequestBody;
import com.linkoog.devtools.http.request.http_request.request_body.RequestBodyPanel;
import com.linkoog.devtools.http.request.common.RequestBodyType;
import com.linkoog.devtools.http.request.http_request.request_header.RequestHeaderPanel;
import com.linkoog.devtools.http.request.http_request.request_param.HttpParamPanel;
import com.linkoog.devtools.intellij.action.JBComboBoxAction;
import org.jetbrains.annotations.NotNull;
import java.util.List;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

public class HttpServiceTestRequestPanel extends JBPanel implements Disposable {

    private Project project;

    private JBEditorTabs editorTabs;

    private HttpParamPanel httpParamPanel;
    private TabInfo paramTabInfo;

    private RequestHeaderPanel requestHeaderPanel;
    private TabInfo headerTabInfo;

    private RequestBodyPanel requestBodyPanel;
    private TabInfo bodyTabInfo;



    public HttpServiceTestRequestPanel(Project project) {
        super(new BorderLayout());
        this.project = project;
        this.editorTabs = new JBEditorTabs(project, this);
        this.setBorder(JBUI.Borders.customLineRight(new JBColor(Gray._214, new Color(30, 31, 34))));



        initHttpParamPanel(project);
        initHttpHeadersPanel(project);
        initHttpBodyPanel(project);
        add(editorTabs, BorderLayout.CENTER);
    }

    public Map<String, List<String>> getParams() {
        return httpParamPanel.getParams();
    }

    public Map<String, List<String>> getHeaders() {
        return requestHeaderPanel.getParams();
    }

    public RequestBody getBody() {
        return requestBodyPanel.buildHttpBody();
    }


    private void initHttpParamPanel(Project project) {
        httpParamPanel = new HttpParamPanel();
        paramTabInfo = new TabInfo(httpParamPanel);
        paramTabInfo.setText("Params");
        paramTabInfo.setObject("Params");
        editorTabs.addTab(paramTabInfo);
    }

    private void initHttpHeadersPanel(Project project) {
        requestHeaderPanel = new RequestHeaderPanel();
        headerTabInfo = new TabInfo(requestHeaderPanel);
        headerTabInfo.setText("Headers");
        headerTabInfo.setObject("Headers");
        editorTabs.addTab(headerTabInfo);
    }

    private void initHttpBodyPanel(Project project) {
        requestBodyPanel = new RequestBodyPanel(project);
        bodyTabInfo = new TabInfo(requestBodyPanel);
        bodyTabInfo.setText("Body");
        bodyTabInfo.setObject("Body");

        DefaultActionGroup tabActionGroup = new DefaultActionGroup();
        tabActionGroup.add(createFormatTypeComboAction());
        bodyTabInfo.setTabPaneActions(tabActionGroup);

        editorTabs.addTab(bodyTabInfo);
    }

    private JBComboBoxAction createFormatTypeComboAction() {
        return new JBComboBoxAction() {
            private JBComboBoxAction formatCombo = this;

            @Override
            public void update(AnActionEvent e) {
                e.getPresentation().setText(RequestBodyType.NONE.getName());
            }

            @Override
            public @NotNull ActionUpdateThread getActionUpdateThread() {
                return ActionUpdateThread.EDT;
            }

            @NotNull
            @Override
            protected DefaultActionGroup createPopupActionGroup(JComponent jComponent) {
                DefaultActionGroup group = new DefaultActionGroup();
                addAction(group, RequestBodyType.NONE);

                group.addSeparator("Text");
                addAction(group, RequestBodyType.PLAIN_TEXT, RequestBodyType.XML, RequestBodyType.JSON);

                group.addSeparator("Structured");
                addAction(group, RequestBodyType.MULTIPART_FORM, RequestBodyType.URL_ENCODED_FORM);

                group.addSeparator("Other");
                addAction(group, RequestBodyType.FILE);
                return group;
            }

            private void addAction(DefaultActionGroup group, RequestBodyType... TYPES) {
                for (RequestBodyType type : TYPES) {
                    group.add(new AnAction(type.getName()) {
                        @Override
                        public void actionPerformed(AnActionEvent anActionEvent) {
                            formatCombo.getTemplatePresentation().setText(anActionEvent.getPresentation().getText());
                            requestBodyPanel.show(type);
                        }
                    });
                }
            }
        };
    }



    @Override
    public void dispose() {
    }
}
